﻿Imports Q7P1TTS.Lib
Imports ComponentFactory.Krypton.Toolkit

Public Class InvestigationForm

    Dim fb As New Complaint()
    Dim invest As New Investigation()
    Public Sub New(ByVal ID As Integer)
        InitializeComponent()
        fb = ComplaintController.load(ID)
        If fb.Investigations.Count > 0 Then
            invest = fb.Investigations(0)
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Dim mainForm As MainForm = Me.FindForm()
        mainForm.GoToForm(New FeedBackForm(fb.ID), "Update Complaint: " & fb.RefNumber)
    End Sub

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If (validateData()) Then
            If cboValiditySelector.SelectedItem.ToString() = "Yes" Then
                invest.IsValid = True
            Else : invest.IsValid = False
            End If
            invest.ComplaintID = fb.ID
            invest.RefNumber = ConvertHelper.GenerateAccount("INVEST")
            invest.ValidReason = txtInvestigation.Text.Trim()
            invest.DriverID = cboDriverSelector.SelectedValue
            Try
                If InvestigationController.save(invest) > 0 Then
                    fb.Status = ComplaintStatus.InvestigationCompleted
                    ComplaintController.save(fb)
                    If MessageBox.Show("Your investigation detail has been successfully saved.") = DialogResult.OK Then
                        Dim mainForm As MainForm = Me.FindForm()
                        mainForm.GoToForm(New FeedBackForm(fb.ID), "Update Complaint: " & fb.RefNumber)
                    End If
                Else
                    MessageBox.Show("There is an unknown error found when saving data. Please try again later.")
                End If
            Catch ex As Exception
                MessageBox.Show("There is an unknown error found when saving data. Please try again later.")
            End Try
        End If
    End Sub

    Private Function validateData() As Boolean
        Dim retval As Boolean = True
        If cboValiditySelector.SelectedItem.ToString = "Yes" Then
            If String.IsNullOrEmpty(txtInvestigation.Text.Trim()) Then
                ErrorProvider1.SetError(txtInvestigation, "Investigation Detail can not be left blank.")
                retval = retval And False
            Else : ErrorProvider1.SetError(txtInvestigation, "")
            End If
            If cboDriverSelector.SelectedIndex = -1 Then
                ErrorProvider1.SetError(cboDriverSelector, "Driver can not be left blank.")
                retval = retval And False
            Else : ErrorProvider1.SetError(cboDriverSelector, "")
            End If
        End If
        Return retval
    End Function

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        For Each c As Control In KryptonPanel1.Controls
            If TypeOf c Is KryptonTextBox Then
                Dim txt As KryptonTextBox = c
                txt.Text = ""
            End If
            If TypeOf c Is KryptonRichTextBox Then
                Dim txt As KryptonRichTextBox = c
                txt.Text = ""
            End If
            If TypeOf c Is KryptonComboBox Then
                Dim cbo As KryptonComboBox = c
                cbo.SelectedIndex = 0
            End If
        Next
    End Sub

    Private Sub InvestigationForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblFbRefNo.Text = fb.RefNumber
        cboDriverSelector.DataSource = DepartmentController.load().Where(Function(d As Department) d.Name = "Driver").Take(1).SingleOrDefault().Employees.ToList()
        cboValiditySelector.SelectedIndex = 0
    End Sub
End Class
